<template>
  <view style="padding: 0.5rem">
    <u-search
        :clearabled="true"
        shape="round"
        :inputAlign="inputAlign"
        v-model="keyword"
        @focus="inputAlign = 'left'"
        @blur="inputAlign = 'center'"
        @change="topicChange"
        @search="searchTopic"
        @custom="searchTopic"
        @clear="showTopicList = true"
        :show-action="true"
        :action-style="{color: '#f63075'}"
        placeholder="搜索话题"
        height="30">
    </u-search>
  </view>

  <view v-if="showTopicList" class="u-wrap">
    <view class="u-menu-wrap">
      <scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
        <view v-for="(item,index) in parentList" :key="index" class="u-tab-item"
              :class="[current==index ? 'u-tab-item-active' : '']"
              :data-current="index" @tap.stop="swichMenu(index)">
          <text class="u-line-1">{{ item.name }}</text>
        </view>
      </scroll-view>
      <block v-for="(item,index) in parentList" :key="index">
        <scroll-view scroll-y class="right-box" v-if="current==index">
          <view class="page-view">
            <view class="class-item">
              <scroll-view scroll-y class="item-container">
                <view  @tap.stop="toPostByChannel(item1.id)" v-for="(item1, index1) in childrenList" :key="index1">
                  <view class="thumb-box">
                    <image class="item-menu-image" lazy-load :src="item1.url" ></image>
                    <view>
                      <view class="item-menu-name">{{ item1.name }}</view>
                      <view class="item-menu-number">{{item1.nickname}}</view>
                    </view>
                    <view class="item-menu-btn">进入</view>
                  </view>
                  <view class="comment-wrapper">
                    &nbsp;<up-text :lines="1" color="#7a7979" size="12" :text="item1.description"></up-text>&nbsp;
                    <view style="font-size: 10px;display: flex;align-items: center">
                      <image style="height: 16px;width: 16px;padding-right: 3px" src="https://www.douluodalu.icu:9000/school/static/topic_comment.png"></image>
                      <text>666</text>
                    </view>
                  </view>
                  <u-line></u-line>
                </view>
              </scroll-view>
            </view>
          </view>
        </scroll-view>
      </block>
    </view>
  </view>

  <view v-else class="search-list">
    <scroll-view scroll-y class="item-container">
      <view @tap.stop="toPostByChannel(item1.id)" v-for="(item1, index1) in searchTopicList"  :key="index1">
        <view class="thumb-box-search">
          <image class="item-menu-image" :src="item1.url" ></image>
          <view style="padding-left: 2rem;line-height: 20px">
            <view class="item-menu-name">{{ item1.name }}</view>
            <view class="item-menu-number">{{item1.nickname}}</view>
          </view>
        </view>
        <u-line></u-line>
      </view>
    </scroll-view>
  </view>
</template>

<script>
import {listChildrenChannel, listParentChannel, searchChannel} from "@/api/channel";
import {searchSchool} from "@/api/school";

export default {
  data() {
    return {
      searchTopicList:[],
      showTopicList:true,
      inputAlign: 'center',
      keyword:'',
      parentList: [], // 左侧菜单的栏目数据
      childrenList: [], // 左侧菜单的栏目数据
      scrollTop: 0, //tab 标题的滚动条位置
      current: 0, // 预设当前项的值
      menuHeight: 0, // 左侧菜单的高度
      menuItemHeight: 0, // 左侧菜单item的高度
      parentId:'',
      params : {
        parentId: 1,
        pageSize: 10,
        current: 1
      }
    }
  },
  computed: {},
  created() {
    this.getList();
  },

  methods: {
    async searchTopic(){
      if(this.keyword.trim() == '' || this.keyword.length == 0){
        uni.showToast({
          title: '请输话题名称',
          duration: 2000,
          icon: 'none'
        });
        this.showTopicList = true
        return
      }
      const res = await searchChannel(this.keyword)
      this.searchTopicList = res.data
      this.showTopicList = false
    },
    topicChange(value){
      if(this.keyword == ''|| this.keyword.length == 0){
        this.showTopicList = true
      }
    },
    /** 查询频道信息列表 */
    async getList() {
       const {data:res} = await listParentChannel()
        this.parentList = res;
        await this.getChildrenList(this.parentList[0].id)
    },
    async getChildrenList(parentId) {
      const res  =  await listChildrenChannel(parentId,this.params.pageSize,this.params.current)
        this.childrenList = res.data.records;
    },
    getImg() {
      return Math.floor(Math.random() * 35); // 生成一个随机数
    },
    toPostByChannel(id){
      uni.navigateTo({
        url: '/pages/topic/topicByChannel?id=' + id
      })
    },
    // 点击左边的栏目切换
    async swichMenu(index) {
      this.getChildrenList(this.parentList[index].id)
      if (index == this.current) return; // 如果当前项和点击的项相同，则返回
      this.current = index; // 将当前项设置为点击的项
      // 如果为0，意味着尚未初始化
      if (this.menuHeight == 0 || this.menuItemHeight == 0) {
        await this.getElRect('menu-scroll-view', 'menuHeight'); // 调用 getElRect 方法获取 menu-scroll-view 元素的高度
        await this.getElRect('u-tab-item', 'menuItemHeight'); // 调用 getElRect 方法获取 u-tab-item 元素的高度
      }
      // 将菜单菜单活动item垂直居中
      this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
    },
    // 获取一个目标元素的高度
    getElRect(elClass, dataVal) {
      new Promise((resolve, reject) => {
        const query = uni.createSelectorQuery().in(this);
        query.select('.' + elClass).fields({size: true}, res => {
          // 如果节点尚未生成，res值为null，循环调用执行
          if (!res) {
            setTimeout(() => {
              this.getElRect(elClass);
            }, 10);
            return;
          }
          this[dataVal] = res.height; // 将查询结果的高度赋值给 dataVal 对应的 dataVal 值
        }).exec();
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.comment-wrapper{
  margin: 10px 0;
  background-color: rgba(192, 192, 192, 0.11);
  border-radius: 3px;
  height: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2px 5px;
}
.thumb-box-search{
  display: flex;
  align-items: center;
  margin-top: 20rpx;
  padding: 0 30rpx;
}
.u-wrap {
  height: calc(100vh);
  /* #ifdef H5 */
  height: calc(100vh - var(--window-top));
  /* #endif */
  display: flex;
  flex-direction: column;
}

.u-search-box {
  padding: 18rpx 30rpx;
}

.u-menu-wrap {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.u-search-inner {
  background-color: rgb(234, 234, 234);
  border-radius: 100rpx;
  display: flex;
  align-items: center;
  padding: 10rpx 16rpx;
}

.u-search-text {
  font-size: 26rpx;
  color: $u-tips-color;
  margin-left: 10rpx;
}

.u-tab-view {
  width: 200rpx;
  height: 100%;
}

.u-tab-item {
  height: 110rpx;
  background: #f6f6f6;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26rpx;
  color: #444;
  font-weight: 400;
  line-height: 1;
}

.u-tab-item-active {
  position: relative;
  color: #000;
  font-size: 30rpx;
  font-weight: 600;
  background: #fff;
}

.u-tab-item-active::before {
  content: "";
  position: absolute;
  border-left: 4px solid $u-primary;
  height: 32rpx;
  left: 0;
  top: 39rpx;
}

.u-tab-view {
  height: 100%;
}

.right-box {
  background-color: rgb(250, 250, 250);
}

.page-view {
  padding: 16rpx;
}

.class-item {
  margin-bottom: 30rpx;
  background-color: #fff;
  padding: 16rpx;
  border-radius: 8rpx;
}

.item-title {
  font-size: 26rpx;
  color: $u-main-color;
  font-weight: bold;
}

.item-menu-name {
  font-weight: normal;
  font-size: 24rpx;
  color: $u-main-color;
}

.item-menu-number {
  font-size: 20rpx;
  color: rgba(84, 82, 82, 0.65);
}
.item-menu-btn{
  border-radius: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #edf8ff;
  color: #4ea4f9;
  font-size: 24rpx;
  width: 50px;
  height: 22px;
}

.item-container {
  height: 95vh;
  display: flex;
  flex-wrap: wrap;
}

.thumb-box {
  //width: 33.333333%;
  display: flex;
  align-items: center;
  justify-content: space-around;
  //flex-direction: column;
  margin-top: 20rpx;
}

.item-menu-image {
  border: 1px solid #fcf7f7;
  width: 140rpx;
  height: 140rpx;
  border-radius: 10px;
}
</style>
